package com.orderserver.web;

import com.orderserver.mapper.OrderMapper;
import domain.Order;
import domain.User;
import feign.usercloud.UserServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author xiaoxi
 * @ProjectName springclouddemo
 * @PackageName com.orderserver.web
 * @ClassName OrderController
 * @Description TODO
 * @Date 2022/11/14 14:35
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderMapper orderMapper;

//    @Resource
//    private RestTemplate restTemplate;

    @Resource
    private UserServer userServer;

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Order findByIdWithFeign(@PathVariable Long id, HttpServletRequest request) {
        Order order = orderMapper.selectById(id);

//        User user = restTemplate.getForObject("http://user-server/user/" + order.getUserId(), User.class);
        User user = userServer.findById(order.getUserId());
        order.setUser(user);

        log.info("header:{}",request.getHeader("auth"));
        log.info("header:{}",request.getHeader("its"));

        return order;
    }


//    /**
//     * 查询
//     *
//     * @param id
//     * @return
//     */
//    @GetMapping("/{id}")
//    public Order findByIdWithRestTemplate(@PathVariable Long id) {
//        Order order = orderMapper.selectById(id);
//
//        User user = restTemplate.getForObject("http://user-server/user/" + order.getUserId(), User.class);
//        order.setUser(user);
//
//        return order;
//    }
}
